package com.tjresearch.redisdbtasktracker.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

public class DateUtils {
	/**
	 * 
	 * @param 时间
	 * @return 一年前的当天
	 */
	public static Date lastOneYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, -1);
		date = cal.getTime();
		return date;
	}

	public static Date lastOneMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -1);
		date = cal.getTime();
		return date;
	}
	
	public static Date nextOneMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);
		date = cal.getTime();
		return date;
	}

	public static Date get20060101() {
		long l20160101 = anyDayTimeStamp("2016-01-01");
		Date ret = new Date(l20160101);
		return ret;
	}
	
	public static Date get20161231() {
		long l20160101 = anyDayTimeStamp("2016-12-31");
		Date ret = new Date(l20160101);
		return ret;
	}

	public static Date[] getMonthDays(String year, String month) {
		int maxDate = 0;
		Date first = null;
		Calendar cal = Calendar.getInstance();
		try {

			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			first = sdf.parse(year + month + "01");
			cal.setTime(first);
			maxDate = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Date[] dates = new Date[maxDate];
		for (int i = 1; i <= maxDate; i++) {
			cal.set(Calendar.DAY_OF_MONTH, i);
			dates[i - 1] = cal.getTime();
		}

		return dates;
	}

	public static int getDateDiff(Date start, Date end) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(start);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(end);
		int day1 = cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if (year1 != year2) // 同一年
		{
			int timeDistance = 0;
			for (int i = year1; i < year2; i++) {
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年
				{
					timeDistance += 366;
				} else // 不是闰年
				{
					timeDistance += 365;
				}
			}

			return timeDistance + (day2 - day1);
		} else // 不同年
		{
			//System.out.println("判断day2 - day1 : " + (day2 - day1));
			return day2 - day1;
		}
	}
	
	public static Date[] getDateInterval(Date start,Date end)
	{
		int k=getDateDiff(start,end);
		Date[] ret = new Date[k-1];
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(start);
		for(int i=1;i<k;i++)
		{
			cal1.add(Calendar.DAY_OF_MONTH, 1);
			ret[i-1]=cal1.getTime();
		}
		return ret;
	}

	public static void main(String args[]) throws Exception {
		Date[] dates = getMonthDays("2016", "02");
		for (Date date : dates) {
			String s = new SimpleDateFormat("yyyyMMdd").format(date);
			System.out.println(s);
		}
		Date start = new SimpleDateFormat("yyyyMMdd").parse("20160101");
		Date end = new SimpleDateFormat("yyyyMMdd").parse("20160302");
		System.out.println(getDateDiff(start, end));
		Date[] intervals = getDateInterval(start,end);
		for (Date date : intervals) {
			String s = new SimpleDateFormat("yyyyMMdd").format(date);
			System.out.println(s);
		}
		
		
		
	}

	/**
	 * 
	 * @param date
	 *            时间
	 * @param i
	 *            相对于给定的时间的第前几天
	 * @return 前几天的日期
	 */
	public static Date getDayBefore(Date date, int i) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, -i);
		date = cal.getTime();
		return date;
	}

	/**
	 * 将日期字符串转化为毫秒值
	 * 
	 * @param dateStr
	 * @return 毫秒值
	 */
	public static long anyDayTimeStamp(String dateStr) {
		try {
			Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
			return date.getTime();
		} catch (ParseException e) {

			e.printStackTrace();
		}
		return 0L;
	}

	/**
	 * 将时间转换成字符串,没有时分秒
	 * 
	 * @return
	 */
	public static String formatDateString(Date date) {

		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}

	/**
	 * 将时间转换成字符串,有时分秒
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateStringTimestamp(Date date) {

		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}

	public static int dayDiff(Date next, Date now) {
		// TODO Auto-generated method stub
		return 0;
	}

	public static Date[] sliceOneMonthDate(Set<Date> days, String string, String string2, int i) {
		// TODO Auto-generated method stub
		return null;
		
	}

}
